package com.ruoyi.project.gxgw_modules.discountManager.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.gxgw_modules.discountManager.domain.Yhq;
import com.ruoyi.project.gxgw_modules.discountManager.service.IYhqService;
import com.ruoyi.project.gxgw_modules.discountManager.vo.YhqVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券 信息操作处理
 *
 * @author ruoyi
 * @date 2019-01-18
 */
@Controller
@RequestMapping("/gxgw_modules/yhq")
public class YhqController extends BaseController {
    private String prefix = "gxgw_modules/yhq";

    @Autowired
    private IYhqService yhqService;

    @RequiresPermissions("gxgw_modules:yhq:view")
    @GetMapping()
    public String yhq() {
        return prefix + "/yhq";
    }

    /**
     * 查询优惠券列表
     */
    @RequiresPermissions("gxgw_modules:yhq:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YhqVO yhqVO) {
        startPage();
        List<YhqVO> list = yhqService.selectYhqList(yhqVO);
        return getDataTable(list);
    }


    /**
     * 导出优惠券列表
     */
    @RequiresPermissions("gxgw_modules:yhq:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YhqVO yhqVO) {
        List<YhqVO> list = yhqService.selectYhqList(yhqVO);
        ExcelUtil<YhqVO> util = new ExcelUtil<YhqVO>(YhqVO.class);
        return util.exportExcel(list, "yhq");
    }

    /**
     * 新增优惠券
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存优惠券
     */
    @RequiresPermissions("gxgw_modules:yhq:add")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Yhq yhq) {
//        System.out.println("新增------" + yhq);
        return toAjax(yhqService.insertYhq(yhq));
    }

    /**
     * 修改优惠券
     */
    @GetMapping("/edit/{yhqId}")
    public String edit(@PathVariable("yhqId") Integer yhqId, ModelMap mmap) {
        Yhq yhq = yhqService.selectYhqById(yhqId);
//        System.out.println("修改---" + yhq );

        String stt = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, yhq.getStartTime());
        String edt = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, yhq.getEndTime());

        mmap.put("yhq", yhq);
        mmap.put("stt", stt);
        mmap.put("edt", edt);
        return prefix + "/edit";
    }

    /**
     * 修改保存优惠券
     */
    @RequiresPermissions("gxgw_modules:yhq:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Yhq yhq) {

        return toAjax(yhqService.updateYhq(yhq));
    }

    /**
     * 删除优惠券
     */
    @RequiresPermissions("gxgw_modules:yhq:remove")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(yhqService.deleteYhqDelFlagByIds(ids));
    }

}
